<template>
  <div>
    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      :default-expanded-keys="expandKeys"
      :expand-on-click-node="false"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
        <span>
          <el-button
            v-if="data.catLevel <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            添加
          </el-button>
          <el-button type="text" size="mini" @click="() => edit(data)">
            更新
          </el-button>
          <el-button
            v-if="data.children && data.children.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form :model="categoryForm">
        <el-form-item label="类别名称">
          <el-input v-model="categoryForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="categoryForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input
            v-model="categoryForm.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  data() {
    //页面效果;
    return {
      data: [],
      defaultProps: {
        children: "children",
        label: "name"
      },
      dialogVisible: false,
      expandKeys: [],
      categoryForm: {
        name: null,
        icon: null,
        productUnit: null,
        showStatus: 1,
        sort: 0,
        catId: null,
        catLevel: 1
      }
    };
  },
  created() {
    this.getCategory();
  },
  methods: {
    getCategory() {
      this.$http({
        url: this.$http.adornUrl("/product/category/listTree"),
        method: "get"
      }).then(({ data }) => {
        console.log("成功获取的类别数据：", data.page);
        this.data = data.page;
        // this.expandKeys = [3];
      });
    },
    append(data) {
      this.type = false;
      console.log("添加", data);
      this.dialogVisible = true;
      this.categoryForm.parentCid = data.catId;
      this.categoryForm.catLevel = data.catLevel + 1;
      this.categoryForm.showStatus = 1;
      this.categoryForm.sort = 0;
    },
    addDialog() {
      // 添加三级分类的类别信息
      this.$http({
        url: this.$http.adornUrl("/product/category/save"),
        method: "post",
        data: this.$http.adornData(this.categoryForm, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "数据添加成功",
            type: "success"
          });
          this.dialogVisible = false; // 关闭弹出窗口 // 重新加载所有的菜单数据
          this.getCategory(); // 设置默认展开的父节点信息
          this.expandKeys = [this.categoryForm.parentCid];
        } else {
          this.$message.error(data.msg);
        }
      });
    },

    edit(data) {
      this.type = true;
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "post",
        data: this.$http.adornData(this.categoryForm, false)
      }).then(({ data }) => {
        this.dialogVisible = true;
        this.categoryForm.name = data.category.name;
        this.categoryForm.productUnit = data.category.productUnit;
        this.categoryForm.icon = data.category.icon;
        this.categoryForm.catLevel = data.category.catLevel;
        this.categoryForm.parentCid = data.category.parentCid;

        this.categoryForm.catId = data.category.catId;
        this.categoryForm.showStatus = 1;
        this.categoryForm.sort = 0;
      });
    },
    editDialog() {
      // 添加三级分类的类别信息
      this.$http({
        url: this.$http.adornUrl("/product/category/update"),
        method: "post",
        data: this.$http.adornData(this.categoryForm, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "数据更新成功",
            type: "success"
          });
          this.dialogVisible = false; // 关闭弹出窗口 // 重新加载所有的菜单数据
          this.getCategory(); // 设置默认展开的父节点信息
          this.expandKeys = [this.categoryForm.parentCid];
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    submitForm() {
      if (this.type) {
        this.editDialog();
      } else {
        this.addDialog();
      }
    },
    remove(node, data) {
      console.log("删除", node, data);
      this.$confirm(`是否确认删除【${data.name}】记录?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let ids = [data.catId];
          // 把删除的请求提交到后台服务
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success"
              });
              // 重新加载所有的菜单数据
              this.getCategory();
              // 设置默认展开的父节点信息
              this.expandKeys = [node.parent.data.catId];
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style></style>
